
//https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=23277&ru=%2Fpractice%2F8ee967e43c2c4ec193b040ea7fbb10b8&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D13
//思想1:笨方法
//思想2:递归快速幂，效率高的方法，找规律-》递归实现；//https://zhuanlan.zhihu.com/p/95902286
        // 这里用了比较多的& >> 运算来提高计算效率；

#include <limits>
#include <vector>
#include <math.h>

using namespace std;

class Solution {
  public:

    bool isEqual(double a, double b) {
        return std::fabs(a - b) < std::numeric_limits<double>::epsilon();
    }

    double Power(double b, int n) {

        if (isEqual(b, 0.0) && n < 0) {
            return 0.0;
        } 
        
        unsigned int abs_n = std::abs(n); 
        double result = 1.0;
        for (int i = 1 ;i <= abs_n;++i) {
            result *=b;
        }

        if (n<0) {
            result = 1.0/result;
        }
        return  result;
    }
};

#include <bits/types/struct_tm.h>
class Solution {
  public:

    bool isEqual(double a, double b) {
        return std::abs(a - b) < std::numeric_limits<double>::epsilon();
    }

    double recursive_Power(double b, int n) {
        
        if (n == 0) {
            return  1;
        }
        else if (n&0x01) {
            return recursive_Power(b, n-1)*b;
        }
        else {
            double tmp = recursive_Power(b, n>>1);
            return tmp*tmp;
        }
    }
    double Power(double b, int n) {
        if (isEqual(b, 0.0) && n < 0) {
            return 0.0;
        } 
        double res = recursive_Power(b,std::abs(n));
        if(n<0) {
            res = 1/res;
        }
        return  res;
    }
};